<?php

namespace Admin\Controller;

class SmsController extends CommonController
{
    public function _initialize()
    {
        parent::_initialize();
        //$config = M('Config')->fetchSql(true)->select();
        $config = D('Config')->get_config();
        $this->assign('show', $config);
    }

    // 配置
    public function config()
    {
        $this->display();
    }

    // 配置保存
    public function edit()
    {
        if (IS_POST) {
            $data   = M('Config');
            $config = I('post.');
            //$intval_array=array('YTATA_GOLD_SEND_MB','YTATA_GOLD_QUANTIFIER_ANONYMOUS','YTATA_MAP_ZOOM');
            foreach ($config as $key => $v) {
                $where['name']  = "{$key}";
                $savev['value'] = "{$v}";
                if ($where['name'] == 'YTATA_SITE_DOMAIN') {
                    $savev['value'] = rtrim($savev['value'], '/');
                }
                if ($where['name'] == 'YTATA_GOLD_SEND_MB') {
                    $savev['value'] = intval($savev['value']) < 0 ? 0 : intval($savev['value']);
                }
                if ($where['name'] == 'YTATA_GOLD_QUANTIFIER_ANONYMOUS') {
                    $savev['value'] = intval($savev['value']) < 0 ? 0 : intval($savev['value']);
                }


                $data->where($where)->save($savev);
            }
            if (!ytata_refresh_cache('config')) {
                $this->error('生成缓存失败！');
            }
            $this->success('保存完成');
        } else {
            $this->error('参数错误');
        }
    }

    // 模板
    public function tpl()
    {
        $list = M('SmsTpl')->select();
        $this->assign('list', $list);
        $this->display();
    }

    // 模板保存
    public function tpl_save()
    {
        $id    = I('post.id');
        $tplid = I('post.tplid');
        $num   = 0;
        $data  = M("SmsTpl");
        if (is_array($id) && count($id) > 0) {
            foreach ($id as $k => $v) {
                unset($map);
                $map['id']    = intval($v);
                $map['tplid'] = trim($tplid[$k]);
                $data->save($map);
                $num++;

            }
        }
        if ($num > 0) {
            $this->success('成功保存' . $num . '个模板！');
        } else {
            $this->error('未保存任何分类！');
        }
    }

}